package com.kyzh.core.pager.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.News
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.GlobalKeys
import com.kyzh.core.R
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.startActivity
import kotlinx.android.synthetic.main.fragment_homefinditem.*


class HomeFindItemFragment : Fragment() {
    private var classId: Int = 0
    private var page:Int=1

    companion object {
        @JvmStatic
        fun newInstance(classid: Int) =
                HomeFindItemFragment().apply {
                    arguments = Bundle().apply {
                        putInt("classid", classid)
                    }
                }
    }


    private val beans = arrayListOf<News>()
    private val adapter = VideoLikeAdapter(R.layout.item_home_find,beans)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            classId = it.getInt("classid")
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_homefinditem, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()
    }

    private fun initView() {
        rvNews.adapter = adapter
        initData()
    }

    private fun initData() {
        adapter.loadMoreModule.setOnLoadMoreListener {
            page++
            com.gushenge.core.requests.HomeRequest. findNews( page,classId){ data, p, max_p->

                    adapter.addData(data)
                    adapter.loadMoreModule.loadMoreComplete()
                    if (page >= max_p) {
                        adapter.loadMoreModule.loadMoreEnd()
                    }
                }
            }
        page=1
        com.gushenge.core.requests.HomeRequest. findNews(page,classId){ data, p, max_p->
                adapter.setNewInstance(data)
                if (page >= max_p) {
                    adapter.loadMoreModule.loadMoreEnd()
                }
            }
        }
    private inner class VideoLikeAdapter(layout:Int,beans:ArrayList<News>): BaseQuickAdapter<News, BaseViewHolder>(layout,beans),LoadMoreModule  {

        override fun convert(holder: BaseViewHolder, item: News) {
            holder.setText(R.id.tvTitle,item.title).setText(R.id.time,item.title)
            holder.getView<ImageView>(R.id.ivImage).loadImage(item.image)
            holder.itemView.setOnClickListener {
                startActivity<BrowserActivity>(GlobalKeys.TITLE to item.title, GlobalKeys.LINK to GlobalConsts.NEWS_XQ + item.id)
            }
        }
    }
//    private inner class VideoLikeAdapter(layout:Int,beans:ArrayList<News>): BaseQuickAdapter<News, BaseViewHolder>(layout,beans) , LoadMoreModule {
//
//        init {
//            addChildClickViewIds(
//                    R.id.ivQuxiao,
//                    R.id.tvDetail
//            )
//        }
//        override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
//            DataBindingUtil.bind<ItemHomeFindBinding>(viewHolder.itemView)
//        }
//        override fun convert(holder: BaseViewHolder, item: News) {
//            holder.getBinding<ItemHomeFindBinding>()?.data = item
//
//            holder.itemView.setOnClickListener {
//                startActivity<BrowserActivity>(GlobalKeys.TITLE to item.title, GlobalKeys.LINK to GlobalConsts.NEWS_XQ + item.id)
//            }
//        }
//    }

}